using System;
using UMC.Web;
using UMC.Data;

using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "Open", Auth = WebAuthType.Admin, Desc = "打开工作流")]
    public class DesignOpenActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var SId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
             {
                 var grid = new TaskFormGridDialog()
                 {
                     Title = "打开工作流"
                 };
                 //  grid.IsPage = true;
                 grid.CloseEvent = "WorkFlow.Design";
                 if (request.IsMaster)
                 {
                     grid.Menu("新建", request.Model, "Form", "News");
                 }
                 return grid;
             })) ?? 0;
            // var formEntity = TaskRuntime.Database.ObjectEntity<TaskForm>()
            // .Where.And().Equal(new TaskForm { TaskId = SId }).Entities;


            var form = DataFactory.Instance().TaskForm(SId);// formEntity.Single();

            var Task = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Task", g =>
            {
                if (form.DesignId.HasValue == false || DataFactory.Instance().TaskDesign(form.DesignId.Value) == null)
                {
                    TaskInstance instance = new TaskInstance
                    {
                        InstanceId = Utility.NewId(),
                        TaskId = form.TaskId.Value,
                        Text = String.Format("{0} v1.0", form.Caption),
                    };
                    instance.Shapes.Add(new UMC.WorkFlow.Subassembly.Shape() { Text = "开始", Id = "spe0" });

                    var body = UMC.Data.JSON.Serialize(instance, true);
                    // UMC.WorkFlow.TaskRuntime.Database.ObjectEntity<TaskDesign>()
                    // .Insert
                    DataFactory.Instance().Put(new TaskDesign
                    {
                        Id = instance.InstanceId,
                        Name = instance.Text,
                        TaskId = form.TaskId,
                        Context = body,
                        UpdateDate = DateTime.Now,
                        Username = this.Context.Token.Username
                    });
                    DataFactory.Instance().Put(new TaskForm { TaskId = form.TaskId, DesignId = instance.InstanceId });
                    this.Context.Send("WorkFlow.Design", new WebMeta().Put("Task", UMC.Data.JSON.Expression(body)), true);



                }
                return this.DialogValue(form.DesignId.ToString());
            })) ?? 0;


            var json = TaskManager.Design(Task);//, UMC.Data.Utility.GetUsername());

            var inst = JSON.Deserialize<TaskInstance>(json);
            inst.TaskId = form.TaskId.Value;
            inst.InstanceId = Task;
            this.Context.Send("WorkFlow.Design", new WebMeta().Put("Task", UMC.Data.JSON.Expression(json)), true);

        }
    }
}